package com.example.demo.service.impl;

import com.example.demo.entity.Coalminecar;
import com.example.demo.handle.MineCarListParseHandler;
import com.example.demo.mapper.CoalminecarMapper;
import com.example.demo.service.CoalminecarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-28
 */
@Service
public class CoalminecarServiceImpl extends ServiceImpl<CoalminecarMapper, Coalminecar> implements CoalminecarService {

    @Autowired
    private CoalminecarMapper coalminecarMapper;

    @Override
    public Integer insertMineCarData(String mineCarXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            MineCarListParseHandler mineCarListParseHandler = new MineCarListParseHandler();
            try {
                // 读取xml文件
                parser.parse(mineCarXml, mineCarListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + mineCarListParseHandler.getCoalminecarArrayList().size() + "个节点被存储");
            for (Coalminecar coalminecar : mineCarListParseHandler.getCoalminecarArrayList()) {
                System.out.println(coalminecar.getCsMineCode());
                System.out.println(coalminecar.getCsDataTime());
                System.out.println(coalminecar.getOmWeighterCode());
                System.out.println(coalminecar.getOmMcarCount());
                System.out.println(coalminecar.getOmOutputHour());
                System.out.println(coalminecar.getOmOutputDay());
                System.out.println(coalminecar.getOmOutputMonth());
                System.out.println(coalminecar.getOmOutputYear());
                System.out.println("----finish----");
                coalminecarMapper.insertMineCarData(coalminecar.getCsMineCode(),coalminecar.getCsDataTime(),coalminecar.getOmWeighterCode(),coalminecar.getOmMcarCount(),coalminecar.getOmOutputHour(),coalminecar.getOmOutputDay(),coalminecar.getOmOutputMonth(),coalminecar.getOmOutputYear());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
